package com.findmeastore.action;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;

import com.findmeastore.bc.IBusinessDetailsBC;
import com.findmeastore.bc.IProductBc;
import com.findmeastore.bo.BusinessEntity;
import com.findmeastore.bo.CategoryBo;
import com.findmeastore.bo.ProductBo;
import com.findmeastore.bo.RelBusinessCategory;
import com.findmeastore.bo.RelBusinessProduct;
import com.findmeastore.dto.CategoryDto;
import com.findmeastore.dto.RelBusinessCatProdDto;
import com.opensymphony.xwork2.ActionSupport;

public class CategoryProductRelAction extends ActionSupport {
	private IBusinessDetailsBC businessDetailsBc;
	private IProductBc productBc;
	private List<CategoryDto> selectedCategoryList = new ArrayList<CategoryDto>();
	private Integer businessId;
	
	private List<RelBusinessCatProdDto> relBusCatProdList = new ArrayList<RelBusinessCatProdDto>();

	public String execute(){
		return "success";
	}
	
	public String addCatProdRel(){
		if(businessId == null && businessId < 1) {
			throw new RuntimeException("Invalid business id");
		}
		
		BusinessEntity entity = businessDetailsBc.getBusinessById(businessId);
		
		if(entity == null) {
			throw new RuntimeException("Invalid input");
		}
		Set<RelBusinessCategory> relBusinessCategories = entity.getRelBusinessCategories();
		
		if(CollectionUtils.isNotEmpty(relBusinessCategories)) {
			// preparing existing selected product Ids
			for(RelBusinessCategory relBusinessCategory : relBusinessCategories) {
				CategoryBo category = relBusinessCategory.getMasterCategory();
				RelBusinessCatProdDto dto = new RelBusinessCatProdDto();
				dto.setCategoryName(category.getName());
				dto.setBusinessCatRelId(relBusinessCategory.getId());
				dto.setBusinessId(entity.getId());
				dto.setCategoryId(category.getId());
				dto.setAllProducts(category.getProducts());
				Set<ProductBo> selectedProducts = new HashSet<ProductBo>();
				Set<RelBusinessProduct> relBusinessProducts = relBusinessCategory.getRelBusinessProducts();
				for(RelBusinessProduct relBusinessProduct : relBusinessProducts) {
					selectedProducts.add(relBusinessProduct.getMasterProduct());
				}
				dto.setSelectedProducts(selectedProducts);
				relBusCatProdList.add(dto);
			}
		}
		return "success";
	}

	public String save(){
		if(businessId == null || businessId < 1) {
			throw new RuntimeException("Invalid input");
		}
		
		BusinessEntity entity = businessDetailsBc.getBusinessById(businessId);
		if(entity == null) {
			throw new RuntimeException("Invalid business id");
		}
		// hard coded last modified by id
		businessDetailsBc.saveBusinessProductsRel(relBusCatProdList, 1);
		return "success";
	}
	
	public void setBusinessDetailsBc(IBusinessDetailsBC businessDetailsBc) {
		this.businessDetailsBc = businessDetailsBc;
	}

	public IBusinessDetailsBC getBusinessDetailsBc() {
		return businessDetailsBc;
	}

	public void setSelectedCategoryList(List<CategoryDto> selectedCategoryList) {
		this.selectedCategoryList = selectedCategoryList;
	}

	public List<CategoryDto> getSelectedCategoryList() {
		return selectedCategoryList;
	}

	public void setBusinessId(Integer businessId) {
		this.businessId = businessId;
	}

	public Integer getBusinessId() {
		return businessId;
	}

	public void setRelBusCatProdList(List<RelBusinessCatProdDto> relBusCatProdList) {
		this.relBusCatProdList = relBusCatProdList;
	}

	public List<RelBusinessCatProdDto> getRelBusCatProdList() {
		return relBusCatProdList;
	}
}
